//
//  DesignerPageViewController.m
//  ActiveDeck
//
//  Created by Laura McBride on 5/25/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PageViewController.h"
#import "DesignerPageViewController.h"
#import "AD_Page.h"
#import "AD_Button.h"
#import "AD_Video.h"

#define degreesToRadian(x) (M_PI * (x) / 180.0)

UIButton *submitButton;


@implementation DesignerPageViewController
@synthesize imageSets, cmvc;


- (void)AddButton{
	
    int i=0;
	for(AD_Button *multiButton in pageOB.mut_Arr_Button)
	{
		UIImage *offImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:multiButton.imageOff]];
		UIImage *onImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:multiButton.imageOn]];
		CGRect buttonFrame = CGRectMake(multiButton.left, multiButton.top, multiButton.width, multiButton.height);
		NSArray *condition = [multiButton.Condition componentsSeparatedByString:@"=="];
        
		UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[newButton setFrame:buttonFrame];
		[newButton setTag:i++];
		[newButton setBackgroundImage:offImage forState:UIControlStateNormal];
		[newButton setBackgroundImage:onImage forState:UIControlStateHighlighted];
		[newButton setBackgroundImage:onImage forState:UIControlStateSelected];
		[newButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
		
        for(AD_Action *action in multiButton.actions){
            if([action isKindOfClass:[Action_Submit class]] || [action isKindOfClass:[Action_Next class]]){
                newButton.enabled = TRUE;
                submitButton = newButton;
                break;
            }
        }
        
//		if([multiButton.buttonType isEqualToString:@"Submit"] || [multiButton.buttonType isEqualToString:@"Next"])
//		{
//			newButton.enabled = NO;
//			submitButton = newButton;
//		}
		
        [newButton setHidden:[self setCondition:condition]];
		[self.view	addSubview:newButton]; //Test memory removed
	}
}

-(void)buttonPress:(id)sender
{
	UIButton *pressButton = (UIButton *)sender;
	AD_Button *multiButton = (AD_Button*)[pageOB.mut_Arr_Button objectAtIndex:pressButton.tag];
	
    [self buttonAction:multiButton];
}


- (void)AddVideo{
    for(AD_Video *multiVideo in pageOB.mut_Arr_Videos)
        [self readyPlayer:multiVideo];
}

- (void) readyPlayer:(AD_Video *)video
{    
    cmvc = [[CustomMovieViewController alloc] init];
    
    [cmvc set_cmvcDelegate:self];
    
    cmvc.autoplay = !isForPDF;
    
    [cmvc readyPlayer:video.mediaName :CGRectMake(video.left, video.top, video.width, video.height) :video.orientation :NO :Kiosk.enabled];
    
    
    [[self view] addSubview:cmvc];
    
    mp = [cmvc mp];
    
    [cmvc release];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    LABTlinkSets = [[NSMutableArray alloc] init];
    
    [self addImage];

    //BG_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:pageOB.imageBG]];
    [self AddVideo];
	[self AddButton];
    [self AddLayer];
    //[self readyPlayer];
    [super viewDidLoad];
}

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[cmvc mp] play];
}

- (void) viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[cmvc mp] pause];
}

-(void)rotateForMovie:(BOOL)shouldRotate{
    [_delegate rotateForMovie:shouldRotate];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    if(cmvc != nil){ 
        if(![cmvc isStopped]){
            [[cmvc mp] stop];
        }
    }

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [LABTlinkSets release];
    [super dealloc];
}


@end
